package com.summer.core.web.action.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.cxl.price.util.SummerGlobals;
import com.summer.core.action.BaseAction;
import com.summer.core.container.Const;
import com.summer.core.container.SessionContainer;
import com.summer.core.web.bean.Admin;
import com.summer.core.web.service.AdminService;
import com.taobao.api.internal.util.WebUtils;
import com.taobao.api.request.SellercatsListGetRequest;
import com.taobao.api.request.UserSellerGetRequest;
import com.taobao.api.response.UserSellerGetResponse;

@ParentPackage("admin")
@Results({
	@Result(name="head",location="menu!getMainMenu.action",type="redirect"),
	@Result(name="goodsList",location="/goods!list.action",type="redirect")
})
public class AdminAction extends BaseAction<Admin, AdminService> {

	public void setAdmin(Admin admin) {
		setBean(admin);
	}

	public Admin getAdmin() {
		return getBean();
	}

	@Resource
	public void setService(AdminService service) {
		this.service = service;
	}
	
	

	/**
	 * 登录
	 * 
	 * @return
	 * @throws IOException 
	 */
	public String login() throws Exception  {
		String type=SummerGlobals.getProperty("top.type");
		String url=SummerGlobals.getProperty("top."+type+".url");
		String appkey=SummerGlobals.getProperty("top."+type+".appkey");
		String appsecret=SummerGlobals.getProperty("top."+type+".secret");
		String tokenUrl=SummerGlobals.getProperty("top."+type+".tokenUrl");
		String redirectUrlPrefix=SummerGlobals.getProperty("top."+type+".callBack")+"/admin/admin!login.action";
		String code=getRequest().getParameter("code");
		Map<String, String> param = new HashMap<String, String>();
		param.put("grant_type", "authorization_code");
		param.put("code", code);
		param.put("client_id", appkey);
		param.put("client_secret", appsecret);
		param.put("redirect_uri", redirectUrlPrefix);
		param.put("view", "web");
		param.put("state", getRequest().getParameter("state"));
		String responseJson=WebUtils.doPost(tokenUrl, param, 3000, 3000);
		JSONObject jsonObject = JSONObject.fromObject(responseJson);
		SessionContainer container = new SessionContainer();
		container.setSessionid(jsonObject.getString("access_token"));
		getRequest().getSession().setAttribute(Const.SessionContainer,container);
		try {
		UserSellerGetResponse response=topBaseService.getTaobaoResponse(UserSellerGetRequest.class,getRequest());
		container.setUser(response.getUser());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "goodsList";
	}
	
	// 后台主页面
	public String main() {
		return result("main");
	}
	
	 //获得一级菜单

	public String header() {
		return "head";
	}
	
	// 后台菜单
	public String menu() {
		return result("menu");
	}
	
	// 后台中间(显示/隐藏菜单)
	public String middle() {
		return result("middle");
	}
	
	

	/**
	 * 增加用户
	 * 
	 * @return
	 */
	public String addAdmin() {
		getService().save(getBean());
		return "save";
	}

	
	
	
	
	
	
}
